﻿#region Copyright (c) Nicola Gallo All rights reserved.
/*
 * All rights reserved. Reproduction of trasmission of this file, or a portion thereof,
 * is forbidden without prior written permission of Nicola Gallo.
 * 
 * Author: Nicola Gallo
 * Date: 09/04/2011
 * 
 * */
#endregion
namespace PrismMVC.Shell.Data
{
    //.NET References
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// This class represents a factory of <see cref="ViewDataDictionary"/>s.
    /// </summary>
    public class ViewDataDictionaryFactory
    {
        //Fields

        private static ICollection<ViewDataDictionary> _registeredViewDataDictionarys;

        //Properties

        /// <summary>
        /// Gets the registered <see cref="ViewDataDictionary"/> collection.
        /// </summary>
        protected static ICollection<ViewDataDictionary> RegisteredViewDataDictionarys
        {
            get
            {
                if (ViewDataDictionaryFactory._registeredViewDataDictionarys == null)
                {
                    ViewDataDictionaryFactory._registeredViewDataDictionarys = new List<ViewDataDictionary>();
                }
                return ViewDataDictionaryFactory._registeredViewDataDictionarys;
            }
        }

        //Methods

        /// <summary>
        /// Creates a new <see cref="ViewDataDictionary"/> instance.
        /// </summary>
        /// <param name="ViewDataID">ViewDataDictionary identifier.</param>
        /// <returns></returns>
        public static ViewDataDictionary CreateViewDataDictionary(String ViewDataID)
        {
            if (string.IsNullOrWhiteSpace(ViewDataID))
            {
                throw new ArgumentException("ViewDataID paramater can not be equals to either null or empty string.");
            }
            ViewDataDictionary viewDataDictionary = (from item in ViewDataDictionaryFactory.RegisteredViewDataDictionarys
                               where item.ViewDataID.Equals(ViewDataID)
                               select item).FirstOrDefault();
            if (viewDataDictionary == null)
            {
                viewDataDictionary = new ViewDataDictionary(ViewDataID);
                ViewDataDictionaryFactory.RegisteredViewDataDictionarys.Add(viewDataDictionary);
            }
            return viewDataDictionary;
        }
    }
}